using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using LitJson;
using UnityEditor;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.Tools.Editor;

public class LayerCollisionMatrixWindow : EditorWindow
{
    [MenuItem("GTATools/碰撞矩阵配置", isValidateFunction:false)]
    static void OpenEditor()
    {
        if (EditorApplication.isCompiling || EditorUtility.scriptCompilationFailed)
        {
            EditorUtility.DisplayDialog("Error", "Compile Error", "Ok");
            return;
        }

        EditorWindow.GetWindow<LayerCollisionMatrixWindow>().Close();
        EditorWindow.GetWindow<LayerCollisionMatrixWindow>().Show();
    }

    private const int _MaxLayerCount = 32;
    
    private bool[][] _CollisionMaxtrix;
    private string _ExportFilePath = string.Empty;
    
    public GUIStyle k_ACButton = new GUIStyle("AC Button");
    
    private Vector2 _ScrollPosition;

    void Awake()
    {
        this.minSize = new Vector2(700, 750);
        this.title = "碰撞矩阵";
        
        _ExportFilePath = Path.Combine(Application.dataPath, $"ResourcesAssets/Config/LibertyConfig/GTALayerCollisionMatrix.json");
    }
    
    void OnGUI()
    {
        {
            LayerCollisionMatrixGUI2D.Draw(new GUIContent("Layer Collision Matrix"), GetCollision, SetCollision);

            _ExportFilePath = EditorGUILayout.TextField("导出路径:", _ExportFilePath);
            
            if (GUILayout.Button("读取Json", k_ACButton, GUILayout.Height(20)))
            {
                if (File.Exists(_ExportFilePath))
                {
                    string jsonStr = File.ReadAllText(_ExportFilePath);
                    _CollisionMaxtrix = JsonMapper.ToObject<bool[][]>(jsonStr); 
                }
                else
                {
                    UnityEditor.EditorUtility.DisplayDialog("警告", "未找到指定文件", "确定");
                }
            }
            if (GUILayout.Button("导出Json", k_ACButton, GUILayout.Height(20)))
            {
                if (_CollisionMaxtrix != null)
                {
                    string jsonStr = String.Empty;

                    jsonStr += "[\n";
                    for (int i = 0; i < _MaxLayerCount; ++i)
                    {
                        jsonStr += "\t[";
                        for (int j = 0; j < _CollisionMaxtrix[i].Length; ++j)
                        {
                            if (j != _CollisionMaxtrix[i].Length - 1)
                            {
                                jsonStr += $"{_CollisionMaxtrix[i][j].ToString().ToLower()}, ";
                            }
                            else
                            {
                                jsonStr += $"{_CollisionMaxtrix[i][j].ToString().ToLower()}";
                            }
                        }

                        if (i != _MaxLayerCount - 1)
                        {
                            jsonStr += "],\n";
                        }
                        else
                        {
                            jsonStr += "]\n";
                        }
                    }
                    jsonStr += "]";
                    
                    File.WriteAllText(_ExportFilePath, jsonStr);
                    
                    UnityEditor.EditorUtility.DisplayDialog("导出", "导出完成", "确定");
                }
            }
        }
    }

    void InitMatrix()
    {
        if (_CollisionMaxtrix == null)
        {
            _CollisionMaxtrix = new bool[_MaxLayerCount][];
            for (int i = 0; i < _MaxLayerCount; ++i)
            {
                _CollisionMaxtrix[i] = new bool[_MaxLayerCount-i];
            }
        }
    }

    bool GetCollision(int layer1, int layer2)
    {
        InitMatrix();
        
        int i = layer1;
        int j = _MaxLayerCount - layer2 - 1;
        return _CollisionMaxtrix[i][j];
    }

    void SetCollision(int layer1, int layer2, bool hasCollision)
    {
        InitMatrix();
        
        int i = layer1;
        int j = _MaxLayerCount - layer2 - 1;
        Debug.Log($"SetCollision {i}, {j}");
        _CollisionMaxtrix[i][j] = hasCollision;
    }
    
}
